package com.situ.music.service;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
import android.util.Log;

import androidx.annotation.Nullable;

import com.situ.music.activity.MusicPlayActivity;

import java.io.IOException;
import java.util.Timer;
import java.util.TimerTask;

// Service负责提供后台服务，他是没有界面
public class MusicService extends Service {
    private static final String TAG = "MusicService";
    private MediaPlayer mediaPlayer;
    private Timer timer;
    private TimerTask timerTask;

    @Override
    public void onCreate() {
        super.onCreate();
        mediaPlayer = new MediaPlayer();
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return new MusicController();
    }

    // 这个类是返回给绑定上这个Service的Activity使用
    public class MusicController extends Binder{
        /**
         * 播放指定路径的歌曲
         * @param path
         */
        public void init(String path) {
            try {
                if (mediaPlayer == null) {
                    mediaPlayer = new MediaPlayer();
                }
                mediaPlayer.reset();
                // 设置播放歌曲的路径
                mediaPlayer.setDataSource(path);
                mediaPlayer.prepareAsync();
                mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                    @Override
                    public void onPrepared(MediaPlayer mp) {
                        mediaPlayer.start();
                        // 启动定时器
                        startTimer();
                    }
                });
                //mediaPlayer.start();

                // 启动定时器
                // startTimer();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        private void startTimer() {
            // 创建一个定时器
            timer =new Timer();
            // 定时任务
            timerTask = new TimerTask() {
                @Override
                public void run() {
                    if(mediaPlayer == null) {
                        return;
                    }

                    // 拿到歌曲的总时长,返回的是毫秒
                    int duration = mediaPlayer.getDuration();
                    // 当前已经播放的时长
                    int currentPosition = mediaPlayer.getCurrentPosition();
                    Log.d(TAG, "duration: " + duration);
                    Log.d(TAG, "currentPosition: " + currentPosition);
                    Bundle bundle = new Bundle();
                    bundle.putInt("duration", duration);
                    bundle.putInt("currentPosition", currentPosition);
                    Message message = new Message();
                    message.setData(bundle);
                    MusicPlayActivity.handler.sendMessage(message);
                }
            };
            // 初始定时器延迟5毫秒执行timerTask，以后每隔500再执行timerTask
            timer.schedule(timerTask, 5, 500);
        }

        /**
         * pause->play
         */
        public void play() {
            Log.d(TAG, "paly");
            mediaPlayer.start();
        }

        /**
         * play->pause
         */
        public void pause() {
            Log.d(TAG, "pause");
            mediaPlayer.pause();
        }

        public boolean isPlaying () {
            boolean isPlaying = mediaPlayer.isPlaying();
            Log.d(TAG, "isPlaying: " + isPlaying);
            return  isPlaying;
        }


        public void seekTo(int progress) {
            mediaPlayer.seekTo(progress);
        }

        public void releasePlayer() {
            Log.d(TAG, "releasePlayer");
            // 如果已经关闭，就不需要做处理
            if (mediaPlayer == null) {
                return;
            }

            // 停止定时器
            timer.cancel();
            timer = null;
            timerTask.cancel();
            timerTask = null;

           mediaPlayer.stop();
            // 释放资源
            mediaPlayer.release();
            mediaPlayer = null;
            Log.d(TAG, "release end");
        }
    }
}
